from pydantic import BaseModel, validator, EmailStr
from typing import Optional, Dict


class ManufacturerCreate(BaseModel):
    """厂商创建验证器"""
    company_name: str
    contact_person: str
    email: EmailStr
    phone: str
    address: str
    supported_materials: Dict[str, str]
    max_production_capacity: int
    production_lead_time: int
    api_endpoint: Optional[str] = None
    api_key: Optional[str] = None
    
    @validator('company_name')
    def validate_company_name(cls, v):
        if not v or len(v.strip()) == 0:
            raise ValueError('公司名称不能为空')
        if len(v) > 200:
            raise ValueError('公司名称长度不能超过200个字符')
        return v.strip()
    
    @validator('contact_person')
    def validate_contact_person(cls, v):
        if not v or len(v.strip()) == 0:
            raise ValueError('联系人不能为空')
        if len(v) > 50:
            raise ValueError('联系人长度不能超过50个字符')
        return v.strip()
    
    @validator('phone')
    def validate_phone(cls, v):
        if not v or len(v.strip()) == 0:
            raise ValueError('电话不能为空')
        # 移除了长度限制，支持各种格式的电话号码
        return v.strip()
    
    @validator('address')
    def validate_address(cls, v):
        if not v or len(v.strip()) == 0:
            raise ValueError('地址不能为空')
        if len(v) > 500:
            raise ValueError('地址长度不能超过500个字符')
        return v.strip()
    
    @validator('supported_materials')
    def validate_supported_materials(cls, v):
        if not v:
            raise ValueError('支持材质不能为空')
        if not isinstance(v, dict):
            raise ValueError('支持材质必须是字典格式')
        return v
    
    @validator('max_production_capacity')
    def validate_max_production_capacity(cls, v):
        if v <= 0:
            raise ValueError('最大生产能力必须大于0')
        if v > 1000000:
            raise ValueError('最大生产能力不能超过100万')
        return v
    
    @validator('production_lead_time')
    def validate_production_lead_time(cls, v):
        if v <= 0:
            raise ValueError('生产周期必须大于0天')
        if v > 365:
            raise ValueError('生产周期不能超过365天')
        return v


class ManufacturerUpdate(BaseModel):
    """厂商更新验证器"""
    company_name: Optional[str] = None
    contact_person: Optional[str] = None
    email: Optional[EmailStr] = None
    phone: Optional[str] = None
    address: Optional[str] = None
    supported_materials: Optional[Dict[str, str]] = None
    max_production_capacity: Optional[int] = None
    production_lead_time: Optional[int] = None
    api_endpoint: Optional[str] = None
    api_key: Optional[str] = None
    is_active: Optional[bool] = None
    rating: Optional[float] = None
    
    @validator('company_name')
    def validate_company_name(cls, v):
        if v is not None:
            if not v or len(v.strip()) == 0:
                raise ValueError('公司名称不能为空')
            if len(v) > 200:
                raise ValueError('公司名称长度不能超过200个字符')
            return v.strip()
        return v
    
    @validator('contact_person')
    def validate_contact_person(cls, v):
        if v is not None:
            if not v or len(v.strip()) == 0:
                raise ValueError('联系人不能为空')
            if len(v) > 50:
                raise ValueError('联系人长度不能超过50个字符')
            return v.strip()
        return v
    
    @validator('phone')
    def validate_phone(cls, v):
        if v is not None:
            if not v or len(v.strip()) == 0:
                raise ValueError('电话不能为空')
            return v.strip()
        return v
    
    @validator('address')
    def validate_address(cls, v):
        if v is not None:
            if not v or len(v.strip()) == 0:
                raise ValueError('地址不能为空')
            if len(v) > 500:
                raise ValueError('地址长度不能超过500个字符')
            return v.strip()
        return v
    
    @validator('supported_materials')
    def validate_supported_materials(cls, v):
        if v is not None:
            if not v:
                raise ValueError('支持材质不能为空')
            if not isinstance(v, dict):
                raise ValueError('支持材质必须是字典格式')
            return v
        return v
    
    @validator('max_production_capacity')
    def validate_max_production_capacity(cls, v):
        if v is not None:
            if v <= 0:
                raise ValueError('最大生产能力必须大于0')
            if v > 1000000:
                raise ValueError('最大生产能力不能超过100万')
            return v
        return v
    
    @validator('production_lead_time')
    def validate_production_lead_time(cls, v):
        if v is not None:
            if v <= 0:
                raise ValueError('生产周期必须大于0天')
            if v > 365:
                raise ValueError('生产周期不能超过365天')
            return v
        return v
    
    @validator('rating')
    def validate_rating(cls, v):
        if v is not None:
            if v < 0 or v > 5:
                raise ValueError('评分必须在0-5之间')
            return round(v, 1)
        return v


class ManufacturerSearch(BaseModel):
    """厂商搜索验证器"""
    company_name: Optional[str] = None
    contact_person: Optional[str] = None
    email: Optional[EmailStr] = None
    is_active: Optional[bool] = None
    min_rating: Optional[float] = None
    max_rating: Optional[float] = None
    
    @validator('min_rating', 'max_rating')
    def validate_rating_range(cls, v):
        if v is not None:
            if v < 0 or v > 5:
                raise ValueError('评分范围必须在0-5之间')
            return v
        return v